package com.cuoyi.coupon.mall.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuoyi.coupon.mall.model.req.DictionaryListReq;
import com.cuoyi.coupon.mall.model.res.DictionaryResp;
import com.cuoyi.coupon.mall.mybatis.entity.Dictionary;
import com.cuoyi.coupon.mall.mybatis.mapper.DictionaryMapper;
import com.cuoyi.coupon.mall.service.DictionaryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author Mybatis Plus Generator
 * @since 2022-03-09 22:25:29
 */
@Service
public class DictionaryServiceImpl extends ServiceImpl<DictionaryMapper, Dictionary> implements DictionaryService {

    @Resource
    private DictionaryMapper dictionaryMapper;

    @Override
    public List<DictionaryResp> getDataByType(DictionaryListReq dictionaryListReq) {
        LambdaQueryWrapper<Dictionary> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Objects.nonNull(dictionaryListReq.getType()), Dictionary::getType, dictionaryListReq.getType());
        queryWrapper.eq(Dictionary::getStatus, 1);
        List<Dictionary> dictionaries = dictionaryMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(dictionaries)) {
            return new ArrayList<>();
        }
        List<DictionaryResp> dictionaryList = new ArrayList<>();
        dictionaries.forEach(dictionary -> dictionaryList.add(Convert.convert(DictionaryResp.class, dictionary)));
        return dictionaryList;
    }
}
